﻿@using Szmyd.Infiltrator.Autofac
@model Szmyd.Infiltrator.ViewModels.ProfilingInfoViewModel
@helper DisplayItemRows(IEnumerable<ProfilingInfo> items, int level) {
    foreach (var info in items)
    {
        var children = Model.Info.Where(v => v.ParentId != null && v.ParentId == info.Id && v.TimeSpent.TotalMilliseconds >= 1).OrderByDescending(v => v.TimeSpent).ToList();
        <tr>
            <td>
                <div>
                    </div>
            </td>
            <td>
                <div  style="margin-left: @(20 * level)px">
                    <div>
                        @if(children.Any())
                        {
                            <text>
                            &darr;
                            </text>
                        }
                        <b>@info.MethodName</b>
                    </div>
                    <span class="hint">@T("Target: <i>{0}</i>", info.TargetType.FullName)</span>
                    @if (info.TargetType != info.DeclaringType)
                    {
                        <span class="hint">@T("Declaring: <i>{0}</i>", info.DeclaringType.FullName)</span>
                    }
                </div>
            </td>
            <td>
                @info.CallCount
            </td>
            <td>
                @if (info.TimeSpent.TotalMilliseconds / info.CallCount < 1)
                {
                    @T("<1 ms")
                }
                else
                {
                    @(string.Format("{0:0.} ms", info.TimeSpent.TotalMilliseconds / info.CallCount))
                }
            </td>
            <td>
                @if (info.TimeSpent.TotalMilliseconds < 1)
                {
                    @T("<1 ms")
                }
                else
                {
                    @(string.Format("{0:0.} ms", info.TimeSpent.TotalMilliseconds))
                }
            </td>
        </tr>
        @DisplayItemRows(children, level + 1)
    }
}
<fieldset>
<div class="manage">
    @if (!Model.IsStarted)
    {
        @Html.ActionLink(T("Start profiling").Text, "Start", "Admin", new { Area = "Szmyd.Infiltrator" }, new { @class = "button primaryAction" })
    }
    else
    {
        @Html.ActionLink(T("Stop profiling").Text, "Stop", "Admin", new { Area = "Szmyd.Infiltrator" }, new { @class = "button primaryAction" })
    }
</div>
</fieldset>
    @if (!Model.Info.Any())
    {
        @T("No profiling data available.")
    }
    else
    {
        foreach (var guid in Model.Info.Where(v => v.TimeSpent.TotalMilliseconds >= 1).Select(v => v.ThreadId).Distinct().ToList())
        {
            <fieldset>
            <legend>@T("<b>Thread {0}</b>: {1:0.} ms", guid, Model.Info.Where(v => v.ThreadId == guid).Sum(v => v.TimeSpent.TotalMilliseconds))
            <span class="hint">@T("Displaying only calls lasting longer than 1 ms.")</span>
            </legend>
            <table class="items">
                <thead>
                    <tr>
                        <th scope="col">
                            &nbsp;
                        </th>
                        <th scope="col">@T("Method")
                        </th>
                        <th scope="col">@T("Calls")
                        </th>
                        <th scope="col">@T("Average time")
                        </th>
                        <th scope="col">@T("Time overall")
                        </th>
                    </tr>
                </thead>
                @DisplayItemRows(Model.Info.Where(v => v.ParentId == null && v.ThreadId == guid && v.TimeSpent.TotalMilliseconds >= 1).OrderByDescending(v => v.TimeSpent).ToList(), 0)
            </table>
            </fieldset>
        }
    }

